#include "Util/Base64.h"
#include <iostream>
#include <string>
#include <vector>
#include <cassert>

// 测试辅助函数：验证编码后再解码是否得到原始数据
bool test_encode_decode_cycle(const std::string& input, const std::string& expected_encoded = "") {
    std::cout << "测试输入: \"" << input << "\"" << std::endl;
    
    // 编码
    std::string encoded = base64::encode_string(input);
    std::cout << "编码结果: \"" << encoded << "\"" << std::endl;
    
    // 如果提供了预期的编码结果，验证它
    if (!expected_encoded.empty() && encoded != expected_encoded) {
        std::cout << "编码验证失败! 预期: \"" << expected_encoded << "\"" << std::endl;
        return false;
    }
    
    // 解码
    std::string decoded = base64::decode_string(encoded);
    std::cout << "解码结果: \"" << decoded << "\"" << std::endl;
    
    // 验证解码后的结果是否与原始输入相同
    if (decoded != input) {
        std::cout << "解码验证失败! 解码结果与原始输入不匹配" << std::endl;
        return false;
    }
    
    std::cout << "测试通过!" << std::endl << std::endl;
    return true;
}

// 测试二进制数据的编码和解码
bool test_binary_data() {
    std::cout << "测试二进制数据编码解码:" << std::endl;
    
    // 创建一些二进制数据
    std::vector<uint8_t> binary_data;
    for (int i = 0; i < 256; i++) {
        binary_data.push_back(static_cast<uint8_t>(i));
    }
    
    // 编码
    auto encoded_opt = base64::encode(binary_data);
    if (!encoded_opt) {
        std::cout << "二进制数据编码失败!" << std::endl;
        return false;
    }
    
    std::string encoded = *encoded_opt;
    std::cout << "二进制数据编码结果 (前50字符): " 
              << encoded.substr(0, 50) << "..." << std::endl;
    
    // 解码
    auto decoded_opt = base64::decode(encoded);
    if (!decoded_opt) {
        std::cout << "二进制数据解码失败!" << std::endl;
        return false;
    }
    
    std::vector<uint8_t> decoded = *decoded_opt;
    
    // 验证解码后的结果是否与原始输入相同
    if (decoded.size() != binary_data.size()) {
        std::cout << "二进制数据大小不匹配! 原始: " << binary_data.size() 
                  << ", 解码后: " << decoded.size() << std::endl;
        return false;
    }
    
    for (size_t i = 0; i < binary_data.size(); i++) {
        if (decoded[i] != binary_data[i]) {
            std::cout << "二进制数据不匹配! 位置: " << i 
                      << ", 原始: " << static_cast<int>(binary_data[i]) 
                      << ", 解码后: " << static_cast<int>(decoded[i]) << std::endl;
            return false;
        }
    }
    
    std::cout << "二进制数据测试通过!" << std::endl << std::endl;
    return true;
}

// 测试错误处理
bool test_error_handling() {
    std::cout << "测试错误处理:" << std::endl;
    
    // 测试无效的 base64 输入
    std::string invalid_base64 = "这不是有效的Base64!@#$";
    std::string decoded = base64::decode_string(invalid_base64);
    
    std::cout << "无效输入: \"" << invalid_base64 << "\"" << std::endl;
    std::cout << "解码结果: \"" << decoded << "\"" << std::endl;
    
    if (!decoded.empty()) {
        std::cout << "错误处理测试失败! 应该返回空字符串" << std::endl;
        return false;
    }
    
    std::cout << "错误处理测试通过!" << std::endl << std::endl;
    return true;
}

int main() {
    std::cout << "===== Base64 编码解码测试 =====" << std::endl << std::endl;
    
    // 测试各种长度的字符串
    bool all_passed = true;
    all_passed &= test_encode_decode_cycle("", "");
    all_passed &= test_encode_decode_cycle("f", "Zg==");
    all_passed &= test_encode_decode_cycle("fo", "Zm8=");
    all_passed &= test_encode_decode_cycle("foo", "Zm9v");
    all_passed &= test_encode_decode_cycle("foob", "Zm9vYg==");
    all_passed &= test_encode_decode_cycle("fooba", "Zm9vYmE=");
    all_passed &= test_encode_decode_cycle("foobar", "Zm9vYmFy");
    
    // 测试特殊字符
    all_passed &= test_encode_decode_cycle("你好，世界!");
    all_passed &= test_encode_decode_cycle("Base64 编码测试 @#$%^&*()");
    
    // 测试二进制数据
    all_passed &= test_binary_data();
    
    // 测试错误处理
    all_passed &= test_error_handling();
    
    // 总结
    std::cout << "===== 测试结果 =====" << std::endl;
    if (all_passed) {
        std::cout << "所有测试通过!" << std::endl;
    } else {
        std::cout << "有测试失败!" << std::endl;
    }
    
    return all_passed ? 0 : 1;
}